# coding: utf-8

from sqlalchemy import or_
from db.models import User, SysRole, SysPerm, SysUserRole, SysRolePerm, db
from config import app
from const import roles_data, perms_data

user_perms_data = [
    dict(perm_name=x.name, perm_key=x.KEY) 
    for x in perms_data if 'ROLE_USER' in x.ROLE_KEYS]
book_admin_perms_data = [dict(perm_name=x.name, perm_key=x.KEY) 
                         for x in perms_data if 'ROLE_BOOK_ADMIN' in x.ROLE_KEYS]
sys_admin_perms_data = [dict(perm_name=x.name, perm_key=x.KEY) 
                        for x in perms_data]
with app.app_context():
    # 创建角色
    # roles_data = [
    #     {'role_name': '普通用户', 'role_key': 'ROLE_USER'},
    #     {'role_name': '图书管理员', 'role_key': 'ROLE_BOOK_ADMIN'},
    #     {'role_name': '系统管理员', 'role_key': 'ROLE_SYS_ADMIN'}
    # ]
    for role in roles_data:
        db.session.add(SysRole(**role))

    # 创建权限
    # perms_data = [
    #     {'perm_name': '借书', 'perm_key': 'PERM_BORROW'},
    #     {'perm_name': '还书', 'perm_key': 'PERM_RETURN'},
    #     {'perm_name': '查询书籍', 'perm_key': 'PERM_BOOK_VIEW'},
    #     {'perm_name': '添加书籍', 'perm_key': 'PERM_BOOK_ADD'},
    #     {'perm_name': '编辑书籍信息', 'perm_key': 'PERM_BOOK_EDIT'},
    #     {'perm_name': '处理借书', 'perm_key': 'PERM_BOOK_BORROW'},
    #     {'perm_name': '处理还书', 'perm_key': 'PERM_BOOK_RETURN'},
    #     {'perm_name': '删除书籍', 'perm_key': 'PERM_BOOK_DELETE'},
    #     {'perm_name': '查询用户', 'perm_key': 'PERM_USER_VIEW'},
    #     {'perm_name': '添加用户', 'perm_key': 'PERM_USER_ADD'},
    #     {'perm_name': '修改用户信息', 'perm_key': 'PERM_USER_EDIT'},
    #     {'perm_name': '删除用户', 'perm_key': 'PERM_USER_DELETE'}
    # ]
    for perm in sys_admin_perms_data:
        db.session.add(SysPerm(**perm))

    # 添加系统管理员
    sysuser = User(username='sysadmin', password='123456')
    db.session.add(sysuser)

    # 分配角色给系统管理员
    admin_role = SysRole.query.filter_by(role_name='系统管理员').first()
    db.session.add(admin_role)
    db.session.add(SysUserRole(user_id=sysuser.id, username=sysuser.username, role_id=admin_role.id, role_key=admin_role.role_key))

    # 分配权限给角色
    sys_admin_perms = SysPerm.query.all()
    for perm in sys_admin_perms:
        db.session.add(SysRolePerm(role_id=admin_role.id, role_key=admin_role.role_key, perm_id=perm.id, perm_key=perm.perm_key))


    # 添加图书管理员
    bookuser = User(username='bookadmin', password='123456')
    db.session.add(bookuser)

    # 分配角色给图书管理员
    bookadmin_role = SysRole.query.filter_by(role_name='图书管理员').first()
    db.session.add(bookadmin_role)
    db.session.add(SysUserRole(user_id=bookuser.id, username=bookuser.username, role_id=bookadmin_role.id, role_key=bookadmin_role.role_key))

    # 分配权限给角色
    # bookadmin_perms = SysPerm.query.filter(SysPerm.perm_key.like('PERM_BOOK_%'))
    bookadmin_perms = SysPerm.query.all()
    for perm in bookadmin_perms:
        if perm.perm_key in [x["perm_key"] for x in book_admin_perms_data]:
            db.session.add(SysRolePerm(role_id=bookadmin_role.id, role_key=bookadmin_role.role_key, perm_id=perm.id, perm_key=perm.perm_key))

    # 添加普通用户
    user1 = User(username='zhangpeng', password='123456')
    db.session.add(user1)

    # 分配角色给普通用户
    user1_role = SysRole.query.filter_by(role_name='普通用户').first()
    db.session.add(user1_role)
    db.session.add(SysUserRole(user_id=user1.id, username=user1.username, role_id=user1_role.id, role_key=user1_role.role_key))

    # 分配权限给角色
    # commonuser_perms = SysPerm.query.filter(or_(SysPerm.perm_key=='PERM_BOOK_VIEW', SysPerm.perm_key=='PERM_BORROW', SysPerm.perm_key=='PERM_RETURN'))
    commonuser_perms = SysPerm.query.all()
    for perm in commonuser_perms:
        if perm.perm_key in [x["perm_key"] for x in user_perms_data]:
            db.session.add(SysRolePerm(role_id=user1_role.id, role_key=user1_role.role_key, perm_id=perm.id, perm_key=perm.perm_key))


    # 提交所有更改
    db.session.commit()